package com.linkage.jz.bankruptcy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.ZQUserDetailAuthBaseInfo;
import com.linkage.jz.bankruptcy.bean.ZQUserInfo;
import com.linkage.seapeak.utils.CommonUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;
import jp.wasabeef.glide.transformations.CropCircleTransformation;


public class ProZQUserConfirmToBeListViewAdapter extends BGARecyclerViewAdapter<ZQUserInfo> {
    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();
    private ZQUserDetailAuthBaseInfo mAuthBaseInfo;
    public ProZQUserConfirmToBeListViewAdapter(RecyclerView recyclerView, Context context) {
        super(recyclerView, R.layout.item_zq_tobelist);
    }

    public void setAuth(ZQUserDetailAuthBaseInfo authBaseInfo)
    {
        mAuthBaseInfo =  authBaseInfo;
    }
    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_edit);
        viewHolderHelper.setItemChildClickListener(R.id.iv_manageuser_add);

        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
    }

    @Override
    public void fillData(BGAViewHolderHelper viewHolderHelper, int position, ZQUserInfo userInfo) {
//        viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "111")
//                .setText(R.id.tv_item_bgaswipe_detail,"22");

        try {
            BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_online, View.INVISIBLE);
            viewHolderHelper.setVisibility(R.id.ll_item_bgaswipe_title4, View.INVISIBLE);
            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_title2, View.INVISIBLE);
            viewHolderHelper.setItemChildClickListener(R.id.iv_phone);
            viewHolderHelper.setItemChildClickListener(R.id.iv_email);
            if (userInfo.getContact() == null || "".equalsIgnoreCase(userInfo.getContact())) {
                viewHolderHelper.setVisibility(R.id.iv_email, View.INVISIBLE);
                viewHolderHelper.setVisibility(R.id.iv_phone, View.INVISIBLE);

            } else {
//                viewHolderHelper.setVisibility(R.id.iv_email, View.VISIBLE);
                if(mAuthBaseInfo.isContactCreditor())
                {
                    viewHolderHelper.setVisibility(R.id.iv_email, View.VISIBLE);
                }else{
                    viewHolderHelper.setVisibility(R.id.iv_email, View.INVISIBLE);
                }
                viewHolderHelper.setVisibility(R.id.iv_phone, View.VISIBLE);
                if ("2".equalsIgnoreCase(userInfo.getOnlineStatus())) {
                    viewHolderHelper.setVisibility(R.id.iv_email, View.INVISIBLE);
                }
            }
            swipeItemLayout.setSwipeAble(false);
            if (userInfo.getCurType() == 1) {

                viewHolderHelper.setVisibility(R.id.test, View.GONE);
                viewHolderHelper.setVisibility(R.id.test2, View.VISIBLE);
                viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, View.GONE);
                viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_status, View.VISIBLE);
                viewHolderHelper.setText(R.id.tv_item_bgaswipe_title1, userInfo.getCreditorName());
                viewHolderHelper.setBackgroundRes(R.id.tv_item_bgaswipe_status, R.color.white);
                viewHolderHelper.setTextColorRes(R.id.tv_item_bgaswipe_status, R.color.page_top);
                if ("0".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    if ("0".equalsIgnoreCase(userInfo.getNoticeStatus())) {
                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_status, "已通知");
                        viewHolderHelper.setTextColorRes(R.id.tv_item_bgaswipe_status, R.color.page_top);
                    } else if ("1".equalsIgnoreCase(userInfo.getNoticeStatus())) {
                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_status, "已邮寄通知");
                        viewHolderHelper.setTextColorRes(R.id.tv_item_bgaswipe_status, R.color.page_top);
                    } else if ("2".equalsIgnoreCase(userInfo.getNoticeStatus())) {
                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_status, "邮寄通知");
                        viewHolderHelper.setTextColorRes(R.id.tv_item_bgaswipe_status, R.color.white);
                        viewHolderHelper.setBackgroundRes(R.id.tv_item_bgaswipe_status, R.drawable.login_corner_btn);
                        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_status);
                    } else if ("3".equalsIgnoreCase(userInfo.getNoticeStatus())) {
                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_status, "已拒绝");
                        viewHolderHelper.setTextColorRes(R.id.tv_item_bgaswipe_status, R.color.red);
                    } else if ("4".equalsIgnoreCase(userInfo.getNoticeStatus())) {
                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_status, "未通知");
                        viewHolderHelper.setTextColorRes(R.id.tv_item_bgaswipe_status, R.color.organ);
                    }
                    if(mAuthBaseInfo.isDeleteCreditor()) {
                        swipeItemLayout.setSwipeAble(true);
                    }else{
                        swipeItemLayout.setSwipeAble(false);
                    }
                } else if ("1".equalsIgnoreCase(userInfo.getProcessStatus())) {


                    if ("0".equalsIgnoreCase(userInfo.getVerifStatus())) {
                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_status, "身份待验证");
                    } else if ("1".equalsIgnoreCase(userInfo.getVerifStatus())) {
                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_status, "已接收邀请");
                    } else if ("2".equalsIgnoreCase(userInfo.getVerifStatus())) {
                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_status, "身份验证不通过");
                    }
                    if(mAuthBaseInfo.isDeleteCreditor()) {
                        swipeItemLayout.setSwipeAble(true);
                    }else{
                        swipeItemLayout.setSwipeAble(false);
                    }
                    viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_online, View.INVISIBLE);

                } else {
//                tv_send_notice.setVisibility(View.GONE);

                    if ("5".equalsIgnoreCase(userInfo.getProcessStatus()) || "6".equalsIgnoreCase(userInfo.getProcessStatus()) || "7".equalsIgnoreCase(userInfo.getProcessStatus())
                            || "7".equalsIgnoreCase(userInfo.getProcessStatus()) || "58".equalsIgnoreCase(userInfo.getProcessStatus())) {
                        if ("2".equalsIgnoreCase(userInfo.getOnlineStatus())) {
                            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_online, View.VISIBLE);
                        } else {
                            viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_online, View.INVISIBLE);
                        }
                    }


                    viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_status, View.INVISIBLE);
                    swipeItemLayout.setSwipeAble(false);


                }

                if ("3".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    String tmp = userInfo.getDeclareAmountTotal();


                    viewHolderHelper.setVisibility(R.id.ll_item_bgaswipe_title4, View.VISIBLE);
                    if (tmp != null && !"".equalsIgnoreCase(tmp) && !"null".equalsIgnoreCase(tmp)) {
                        viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_title2, View.VISIBLE);

                        viewHolderHelper.setText(R.id.tv_item_bgaswipe_title2, "申报金额：" + CommonUtils.resetMoney( userInfo.getDeclareAmountTotal()) + "元");
                        viewHolderHelper.setVisibility(R.id.ll_item_bgaswipe_title4, View.INVISIBLE);
                    } else {
                        viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_title2, View.INVISIBLE);
                    }
                }
                if ("4".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
                    float f = Float.parseFloat(userInfo.getAmountDeclared());

                    viewHolderHelper.setVisibility(R.id.ll_item_bgaswipe_title4, View.VISIBLE);
                    viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_title2, View.VISIBLE);
                    String p = decimalFormat.format(f);
                    viewHolderHelper.setText(R.id.tv_item_bgaswipe_title2, "申报金额：" + CommonUtils.resetMoney(p )+ "元");
                    f = Float.parseFloat(userInfo.getConfirmAmount());

                    p = decimalFormat.format(f);
                    if (!userInfo.getAmountDeclared().equalsIgnoreCase(CommonUtils.resetMoney(userInfo.getConfirmAmount()))) {
                        viewHolderHelper.setTextColorRes(R.id.tv_item_bgaswipe_title4, R.color.page_top);
                    }
                    viewHolderHelper.setText(R.id.tv_item_bgaswipe_title4, "0.00");
                }

                if (userInfo.getRedPointFlag() != null &&("true".equalsIgnoreCase(userInfo.getRedPointFlag())||"1".equalsIgnoreCase(userInfo.getRedPointFlag()))) {
                    viewHolderHelper.setVisibility(R.id.unread_count, View.VISIBLE);
//            viewHolderHelper.setVisibility(R.id.img_red2, View.VISIBLE);
//            viewHolderHelper.setText(R.id.img_red2, "  " + userInfo.getMessageCount() + "  ");
                } else {
                    viewHolderHelper.setVisibility(R.id.unread_count, View.GONE);
//            viewHolderHelper.setVisibility(R.id.img_red2, View.GONE);
                }

                if (userInfo.getMessageCount() != null && "0".equalsIgnoreCase(userInfo.getMessageCount())) {
//                    viewHolderHelper.setVisibility(R.id.img_red, View.VISIBLE);
                    viewHolderHelper.setVisibility(R.id.img_red2, View.VISIBLE);
                    viewHolderHelper.setText(R.id.img_red2, "  " + userInfo.getMessageCount() + "  ");
                } else {
//                    viewHolderHelper.setVisibility(R.id.img_red, View.GONE);
                    viewHolderHelper.setVisibility(R.id.img_red2, View.GONE);
                }
                ImageView imageview = viewHolderHelper.getView(R.id.iv_item_bgaswipe_avatar1);
                Glide.with(mContext).load(userInfo.getAvatar())
                        .bitmapTransform(new CropCircleTransformation(mContext))
                        .placeholder(R.drawable.logo)
                        .into(imageview);


            } else {
                viewHolderHelper.setVisibility(R.id.test2, View.GONE);
                viewHolderHelper.setVisibility(R.id.test, View.VISIBLE);
                viewHolderHelper.setVisibility(R.id.tv_item_bgaswipe_edit, View.GONE);

                if ("0".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "通知进程中（" + userInfo.getGroupnum() + "）");
                } else if ("1".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "身份验证中（" + userInfo.getGroupnum() + "）");
                } else if ("2".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "暂未申报（" + userInfo.getGroupnum() + "）");
                } else if ("3".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "暂未审查（" + userInfo.getGroupnum() + "）");
                } else if ("4".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "未有确认债权（" + userInfo.getGroupnum() + "）");
                } else if ("5".equalsIgnoreCase(userInfo.getProcessStatus())) {
                    viewHolderHelper.setText(R.id.tv_item_bgaswipe_title, "已确认债权（" + userInfo.getGroupnum() + "）");

                }


                swipeItemLayout.setSwipeAble(false);


            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
//            swipeItemLayout.setSwipeAble(false);
//        } else {

//        }
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}